package q1620_bestCoordinate;

public class Solution_1 {
    /*
    网络信号最好的坐标
    采用直接遍历的方式首先找出每个信号塔中最大的x y位置，信号最强的一定在(0, 0) -> (x, y)的这个范围内
    为了满足字典序从原点开始遍历，暴力计算即可
     */
    public int[] bestCoordinate(int[][] towers, int radius) {
        int xMax = Integer.MIN_VALUE, yMax = Integer.MIN_VALUE;
        for (int[] tower : towers) {
            xMax = Math.max(xMax, tower[0]);
            yMax = Math.max(yMax, tower[1]);
        }
        int maxSignal = 0, maxR = radius * radius;
        int x = 0, y = 0;
        for (int i = 0; i < xMax; i++) {
            for (int j = 0; j < yMax; j++) {
                int cur = 0;
                for (int[] tower : towers) {
                    int distance = (i - tower[0]) * (i - tower[0]) + (j - tower[1]) * (j - tower[1]);
                    if (distance <= maxR) cur += Math.floor(tower[2] / (1 + Math.sqrt(distance)));
                }
                if (cur > maxSignal) {
                    x = i;
                    y = j;
                    maxSignal = cur;
                }
            }
        }
        return new int[]{x, y};
    }
}
